package vp.remedies.dr.home.activity;

import vp.remedies.dr.home.R;
import vp.remedies.dr.home.db.AndroidSQLiteHelper;
import vp.remedies.dr.home.so.AllRemediesWrapper;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class TypesofDiseasesActivity extends Activity {

	public static String DISEAS_TYPE = "diseas_type";

	// List view
	private ListView listView;

	// Listview Adapter
	ArrayAdapter<String> adapter;

	// Search EditText
	EditText inputSearch;

	public static AllRemediesWrapper typeOfDiseasWrapper = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.types_of_diseases_list);

		String remedieNames[] = { "Ayurveda", "remedies" };

		String type = (String) getIntent().getExtras().get(DISEAS_TYPE);

		remedieNames = getTypesofDiseasesWrapper(type).getNames();

		inputSearch = (EditText) findViewById(R.id.inputSearch);
		listView = (ListView) findViewById(R.id.types_of_diseases_list_items);
		// Adding items to listview
		adapter = new ArrayAdapter<String>(this, R.layout.remedies_name,
				R.id.remedie, remedieNames);

		listView.setAdapter(adapter);

		/**
		 * Enabling Search Filter
		 * */
		inputSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2,
					int arg3) {
				// When user changed the Text
				TypesofDiseasesActivity.this.adapter.getFilter().filter(cs);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable arg0) {
				// TODO Auto-generated method stub
			}
		});

		listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int arg2, long arg3) {
				// On selecting single track get song information
				Intent i = new Intent(view.getContext(),
						WebViewClientActivity.class);

				String remedie = ((TextView) view.findViewById(R.id.remedie))
						.getText().toString();
				Log.i("test", "adapterView::");
				String htmlPageName = typeOfDiseasWrapper.getAllRemediesMap()
						.get(remedie);
				// Toast.makeText(activity, htmlPageName,
				// Toast.LENGTH_LONG).show();

				i.putExtra(WebViewClientActivity.HTML_PAGE_NAME, htmlPageName);
				startActivity(i);
			}
		});
	}

	public static AllRemediesWrapper getTypesofDiseasesWrapper(
			String typeOfDiseas) {
		typeOfDiseasWrapper = new AllRemediesWrapper();
		AndroidSQLiteHelper helper = AndroidSQLiteHelper.getInstance();
		Cursor cursor = helper.getCursorRaw(
				"SELECT  id , name , html, bookmark FROM remedies_en where cat_id='"
						+ typeOfDiseas + "'", null);
		typeOfDiseasWrapper.setIds(new int[cursor.getCount()]);
		typeOfDiseasWrapper.setNames(new String[cursor.getCount()]);
		int count = 0;
		while (cursor.moveToNext()) {
			int id = cursor.getInt(0);
			String name = cursor.getString(1);
			String html = cursor.getString(2);
			int bookmark = cursor.getInt(3);
			typeOfDiseasWrapper.getIds()[count] = id;
			typeOfDiseasWrapper.getNames()[count] = name;
			typeOfDiseasWrapper.getAllRemediesMap().put(name, html);
			count++;
		}
		return typeOfDiseasWrapper;
	}

}
